﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jQueryDotNetLib.Controls
{
    [ToolboxData("<{0}:DatePicker runat=server />")]
    public class DatePicker : TextBox
    {

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientBeforeShow
        {
            get
            {
                if (ViewState["OnClientBeforeShow"] == null) return "";
                return (string)ViewState["OnClientBeforeShow"];
            }

            set
            {
                ViewState["OnClientBeforeShow"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientBeforeShowDay
        {
            get
            {
                if (ViewState["OnClientBeforeShowDay"] == null) return "";
                return (string)ViewState["OnClientBeforeShowDay"];
            }

            set
            {
                ViewState["OnClientBeforeShowDay"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientOnChangeMonthYear
        {
            get
            {
                if (ViewState["OnClientOnChangeMonthYear"] == null) return "";
                return (string)ViewState["OnClientOnChangeMonthYear"];
            }

            set
            {
                ViewState["OnClientOnChangeMonthYear"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientOnClose
        {
            get
            {
                if (ViewState["OnClientOnClose"] == null) return "";
                return (string)ViewState["OnClientOnClose"];
            }

            set
            {
                ViewState["OnClientOnClose"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientOnSelect
        {
            get
            {
                if (ViewState["OnClientOnSelect"] == null) return "";
                return (string)ViewState["OnClientOnSelect"];
            }

            set
            {
                ViewState["OnClientOnSelect"] = value;
            }
        }
















        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string AltField
        {
            get
            {
                if (ViewState["AltField"] == null) return "";
                return (string)ViewState["AltField"];
            }

            set
            {
                ViewState["AltField"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string AltFormat
        {
            get
            {
                if (ViewState["AltFormat"] == null) return "";
                return (string)ViewState["AltFormat"];
            }

            set
            {
                ViewState["AltFormat"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ButtonImage
        {
            get
            {
                if (ViewState["ButtonImage"] == null) return "";
                return (string)ViewState["ButtonImage"];
            }

            set
            {
                ViewState["ButtonImage"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string AppendText
        {
            get
            {
                if (ViewState["AppendText"] == null) return "";
                return (string)ViewState["AppendText"];
            }

            set
            {
                ViewState["AppendText"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ButtonImageOnly
        {
            get
            {
                if (ViewState["ButtonImageOnly"] == null) return false;
                return (bool)ViewState["ButtonImageOnly"];
            }

            set
            {
                ViewState["ButtonImageOnly"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ButtonText
        {
            get
            {
                if (ViewState["ButtonText"] == null) return "";
                return (string)ViewState["ButtonText"];
            }

            set
            {
                ViewState["ButtonText"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ChangeMonth
        {
            get
            {
                if (ViewState["ChangeMonth"] == null) return false;
                return (bool)ViewState["ChangeMonth"];
            }

            set
            {
                ViewState["ChangeMonth"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ChangeYear
        {
            get
            {
                if (ViewState["ChangeYear"] == null) return false;
                return (bool)ViewState["ChangeYear"];
            }

            set
            {
                ViewState["ChangeYear"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CloseText
        {
            get
            {
                if (ViewState["CloseText"] == null) return "";
                return (string)ViewState["CloseText"];
            }

            set
            {
                ViewState["CloseText"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ConstrainInput
        {
            get
            {
                if (ViewState["ConstrainInput"] == null) return true;
                return (bool)ViewState["ConstrainInput"];
            }

            set
            {
                ViewState["ConstrainInput"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string CurrentText
        {
            get
            {
                if (ViewState["CurrentText"] == null) return "";
                return (string)ViewState["CurrentText"];
            }

            set
            {
                ViewState["CurrentText"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string DateFormat
        {
            get
            {
                if (ViewState["DateFormat"] == null) return "";
                return (string)ViewState["DateFormat"];
            }

            set
            {
                ViewState["DateFormat"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string DayNames
        {
            get
            {
                if (ViewState["DayNames"] == null) return "";
                return (string)ViewState["DayNames"];
            }

            set
            {
                ViewState["DayNames"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string DayNamesMin
        {
            get
            {
                if (ViewState["DayNamesMin"] == null) return "";
                return (string)ViewState["DayNamesMin"];
            }

            set
            {
                ViewState["DayNamesMin"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string DayNamesShort
        {
            get
            {
                if (ViewState["DayNamesShort"] == null) return "";
                return (string)ViewState["DayNamesShort"];
            }

            set
            {
                ViewState["DayNamesShort"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime DefaultDate
        {
            get
            {
                if (ViewState["DefaultDate"] == null) return DateTime.Now;
                return (DateTime)ViewState["DefaultDate"];
            }

            set
            {
                ViewState["DefaultDate"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Duration
        {
            get
            {
                if (ViewState["Duration"] == null) return "";
                return (string)ViewState["Duration"];
            }

            set
            {
                ViewState["Duration"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int FirstDay
        {
            get
            {
                if (ViewState["FirstDay"] == null) return 0;
                return (int)ViewState["FirstDay"];
            }

            set
            {
                ViewState["FirstDay"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool GotoCurrent
        {
            get
            {
                if (ViewState["GotoCurrent"] == null) return false;
                return (bool)ViewState["GotoCurrent"];
            }

            set
            {
                ViewState["GotoCurrent"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool HideIfNoPrevNext
        {
            get
            {
                if (ViewState["HideIfNoPrevNext"] == null) return false;
                return (bool)ViewState["HideIfNoPrevNext"];
            }

            set
            {
                ViewState["HideIfNoPrevNext"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool IsRTL
        {
            get
            {
                if (ViewState["IsRTL"] == null) return false;
                return (bool)ViewState["IsRTL"];
            }

            set
            {
                ViewState["IsRTL"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime MaxDate
        {
            get
            {
                if (ViewState["MaxDate"] == null) return DateTime.MaxValue;
                return (DateTime)ViewState["MaxDate"];
            }

            set
            {
                ViewState["MaxDate"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime MinDate
        {
            get
            {
                if (ViewState["MinDate"] == null) return DateTime.MinValue;
                return (DateTime)ViewState["MinDate"];
            }

            set
            {
                ViewState["MinDate"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string MonthNames
        {
            get
            {
                if (ViewState["MonthNames"] == null) return "";
                return (string)ViewState["MonthNames"];
            }

            set
            {
                ViewState["MonthNames"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string MonthNamesShort
        {
            get
            {
                if (ViewState["MonthNamesShort"] == null) return "";
                return (string)ViewState["MonthNamesShort"];
            }

            set
            {
                ViewState["MonthNamesShort"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool NavigationAsDateFormat
        {
            get
            {
                if (ViewState["NavigationAsDateFormat"] == null) return false;
                return (bool)ViewState["NavigationAsDateFormat"];
            }

            set
            {
                ViewState["NavigationAsDateFormat"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string NextText
        {
            get
            {
                if (ViewState["NextText"] == null) return "";
                return (string)ViewState["NextText"];
            }

            set
            {
                ViewState["NextText"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string NumberOfMonths
        {
            get
            {
                if (ViewState["NumberOfMonths"] == null) return "";
                return (string)ViewState["NumberOfMonths"];
            }

            set
            {
                ViewState["NumberOfMonths"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PrevText
        {
            get
            {
                if (ViewState["PrevText"] == null) return "";
                return (string)ViewState["PrevText"];
            }

            set
            {
                ViewState["PrevText"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ShortYearCutoff
        {
            get
            {
                if (ViewState["ShortYearCutoff"] == null) return "";
                return (string)ViewState["ShortYearCutoff"];
            }

            set
            {
                ViewState["ShortYearCutoff"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ShowAnim
        {
            get
            {
                if (ViewState["ShowAnim"] == null) return "";
                return (string)ViewState["ShowAnim"];
            }

            set
            {
                ViewState["ShowAnim"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ShowButtonPanel
        {
            get
            {
                if (ViewState["ShowButtonPanel"] == null) return false;
                return (bool)ViewState["ShowButtonPanel"];
            }

            set
            {
                ViewState["ShowButtonPanel"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ShowCurrentAtPos
        {
            get
            {
                if (ViewState["ShowCurrentAtPos"] == null) return 0;
                return (int)ViewState["ShowCurrentAtPos"];
            }

            set
            {
                ViewState["ShowCurrentAtPos"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ShowMonthAfterYear
        {
            get
            {
                if (ViewState["ShowMonthAfterYear"] == null) return false;
                return (bool)ViewState["ShowMonthAfterYear"];
            }

            set
            {
                ViewState["ShowMonthAfterYear"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ShowOn
        {
            get
            {
                if (ViewState["ShowOn"] == null) return "";
                return (string)ViewState["ShowOn"];
            }

            set
            {
                ViewState["ShowOn"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ShowOptions
        {
            get
            {
                if (ViewState["ShowOptions"] == null) return "";
                return (string)ViewState["ShowOptions"];
            }

            set
            {
                ViewState["ShowOptions"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ShowOtherMonths
        {
            get
            {
                if (ViewState["ShowOtherMonths"] == null) return false;
                return (bool)ViewState["ShowOtherMonths"];
            }

            set
            {
                ViewState["ShowOtherMonths"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int StepMonths
        {
            get
            {
                if (ViewState["StepMonths"] == null) return 1;
                return (int)ViewState["StepMonths"];
            }

            set
            {
                ViewState["StepMonths"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string YearRange
        {
            get
            {
                if (ViewState["YearRange"] == null) return "";
                return (string)ViewState["YearRange"];
            }

            set
            {
                ViewState["YearRange"] = value;
            }
        }


        protected override void Render(HtmlTextWriter writer)
        {

            base.Render(writer);

            System.Text.StringBuilder sb = new StringBuilder();

            sb.Append("<script type=\"text/javascript\">");
            sb.Append("jQuery(document).ready(function($) { var ctl = $(\"#" + this.ClientID + "\"); ctl.datepicker( { ");

            bool needsComma = false;

            if (OnClientBeforeShow != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("beforeShow: function(input) {" + OnClientBeforeShow + "}");
                needsComma = true;
            }
            if (OnClientBeforeShowDay != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("beforeShowDay: function(date) {" + OnClientBeforeShowDay + "}");
                needsComma = true;
            }
            if (OnClientOnChangeMonthYear != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("onChangeMonthYear: function(date) {" + OnClientOnChangeMonthYear + "}");
                needsComma = true;
            }
            if (OnClientOnClose != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("onClose: function(date) {" + OnClientOnClose + "}");
                needsComma = true;
            }
            if (OnClientOnSelect != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("onSelect: function(dateText) {" + OnClientOnSelect + "}");
                needsComma = true;
            }
            if (YearRange != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("yearRange: " + YearRange);
                needsComma = true;
            }
            if (StepMonths != 1)
            {
                if (needsComma) sb.Append(",");
                sb.Append("stepMonths: " + StepMonths);
                needsComma = true;
            }
            if (ShowOtherMonths)
            {
                if (needsComma) sb.Append(",");
                sb.Append("showOtherMonths: true");
                needsComma = true;
            }
            if (ShowOptions != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("showOptions: '" + ShowOptions + "'");
                needsComma = true;
            }
            if (ShowOn != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("showOn: '" + ShowOn + "'");
                needsComma = true;
            }

            if (ShowMonthAfterYear)
            {
                if (needsComma) sb.Append(",");
                sb.Append("showMonthAfterYear: true");
                needsComma = true;
            }

            if (ShowCurrentAtPos != 0)
            {
                if (needsComma) sb.Append(",");
                sb.Append("showCurrentAtPos: " + ShowCurrentAtPos);
                needsComma = true;
            }
            if (ShowButtonPanel)
            {
                if (needsComma) sb.Append(",");
                sb.Append("showButtonPanel: true");
                needsComma = true;
            }
            if (ShowAnim != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("showAnim: '" + ShowAnim + "'");
                needsComma = true;
            }
            if (ShortYearCutoff != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("shortYearCutoff: '" + ShortYearCutoff + "'");
                needsComma = true;
            }
            if (PrevText != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("prevText: '" + PrevText + "'");
                needsComma = true;
            }
            if (NumberOfMonths != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("numberOfMonths: " + NumberOfMonths + "");
                needsComma = true;
            } 
            if (NextText != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("nextText: " + NextText + "");
                needsComma = true;
            }

            if (NavigationAsDateFormat)
            {
                if (needsComma) sb.Append(",");
                sb.Append("navigationAsDateFormat: true");
                needsComma = true;
            }


            if (IsRTL)
            {
                if (needsComma) sb.Append(",");
                sb.Append("isRTL: true");
                needsComma = true;
            }
            if (HideIfNoPrevNext)
            {
                if (needsComma) sb.Append(",");
                sb.Append("hideIfNoPrevNext: true");
                needsComma = true;
            }
            if (GotoCurrent)
            {
                if (needsComma) sb.Append(",");
                sb.Append("gotoCurrent: true");
                needsComma = true;
            }
            if (FirstDay != 0)
            {
                if (needsComma) sb.Append(",");
                sb.Append("firstDay: " + FirstDay);
                needsComma = true;
            }
            if (Duration != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("duration: " + Duration + "");
                needsComma = true;
            }

            if (CurrentText != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("currentText: '" + CurrentText + "'");
                needsComma = true;
            }

            if (!ConstrainInput)
            {
                if (needsComma) sb.Append(",");
                sb.Append("constrainInput: false");
                needsComma = true;
            }
            if (CloseText != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("closeText: '" + CloseText + "'");
                needsComma = true;
            }
            if (ChangeYear)
            {
                if (needsComma) sb.Append(",");
                sb.Append("changeYear: true");
                needsComma = true;
            }
            if (ChangeMonth)
            {
                if (needsComma) sb.Append(",");
                sb.Append("changeMonth: true");
                needsComma = true;
            }
            if (ButtonText != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("buttonText: '" + ButtonText + "'");
                needsComma = true;
            }

            if (ButtonImageOnly)
            {
                if (needsComma) sb.Append(",");
                sb.Append("buttonImageOnly: true");
                needsComma = true;
            }
            if (ButtonImage != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("buttonImage: '" + ButtonImage + "'");
                needsComma = true;
            }
            if (AppendText != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("appendText: '" + AppendText + "'");
                needsComma = true;
            }
            if (AltFormat != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("altFormat: '" + AltFormat + "'");
                needsComma = true;
            }
            if (AltField != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("altField: '" + AltField + "'");
                needsComma = true;
            }

            if (MonthNamesShort != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("monthNamesShort: " + MonthNamesShort + "");
                needsComma = true;
            }

            if (MonthNames != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("monthNames: " + MonthNames + "");
                needsComma = true;
            }

            if (DayNamesShort != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("dayNamesShort: " + DayNamesShort + "");
                needsComma = true;
            }


            if (DayNamesMin != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("dayNamesMin: " + DayNamesMin + "");
                needsComma = true;
            }


            if (DayNames != "")
            {
                if (needsComma) sb.Append(",");
                sb.Append("dayNames: " + DayNames + "");
                needsComma = true;
            }


            ///special cases for dates
            ///


            System.Globalization.CultureInfo clientCulture = System.Globalization.CultureInfo.CurrentCulture;

            if (Page.Request.UserLanguages != null && Page.Request.UserLanguages.Length >= 0)
                clientCulture = System.Globalization.CultureInfo.GetCultureInfo(Page.Request.UserLanguages[0]);

            string _DefaultDateFormat = "MM/dd/yy";

            if (DateFormat != "") 
            {
                if (needsComma) sb.Append(",");
                sb.Append("dateFormat: '" + DateFormat + "'");
                needsComma = true;
                _DefaultDateFormat = DateFormat.Replace("m", "M");
            }



            if (MinDate != DateTime.MinValue)
            {
                if (needsComma) sb.Append(",");
                sb.Append("minDate: '" + MinDate.Date.ToString(_DefaultDateFormat) + "'");
                needsComma = true;
            }
            if (MaxDate != DateTime.MaxValue)
            {
                if (needsComma) sb.Append(",");
                sb.Append("maxDate: '" + MaxDate.Date.ToString(_DefaultDateFormat) + "'");
                needsComma = true;
            }

            if (DefaultDate.Date != DateTime.Now.Date)
            {
                if (needsComma) sb.Append(",");
                sb.Append("defaultDate: '" + DefaultDate.Date.ToString(_DefaultDateFormat) + "'");
                needsComma = true;
            }

            sb.Append("} ); });");
            sb.Append("</script>");

            writer.Write(sb.ToString());

        }
    }
}